package jim.org;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import util.Attributes;
import util.Profile;

/**
 * @author Jonas Dietz and Ramon Victor from the Cologne University of Applied Sciences
 * @since 2012
 * This is the "Create new Profile" Frame Class.
 */
@SuppressWarnings("serial")
public class NewProfile extends JFrame {

    private JPanel contentPane;
    private JTabbedPane tabbedPane;
    private JPanel panel;
    private JLabel lblProtocol;
    private JLabel lblUser;
    private JLabel lblPassword;
    private JComboBox protocolComboBox;
    private JTextField UserTextField;
    private JPasswordField PasswordField;
    private JPanel panel_1;
    private JLabel lblServer;
    private JLabel lblPort;
    private JLabel lblSecurity;
    private JTextField ServerTextField;
    private JTextField PortTextField;
    private JComboBox SecuritycomboBox;
    private JButton btnAdd;
    private JButton btnCancel;
    private JLabel lblDomain;
    private JTextField DomainTextField;
    private JRadioButton rdbtnSavePassword;

    /**
     * Create the frame.
     */
    public NewProfile() {
	setResizable(false);
	setTitle(Attributes.rs.getString("createNewProfile"));
	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	setBounds(100, 100, 320, 236);

	contentPane = new JPanel();
	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	contentPane.setBackground(Attributes.backgroundColor);
	contentPane.setForeground(Attributes.textColor);
	setContentPane(contentPane);
	contentPane.setLayout(null);

	tabbedPane = new JTabbedPane(SwingConstants.TOP);
	tabbedPane.setBounds(12, 12, 292, 142);
	tabbedPane.setBackground(Attributes.backgroundColor);
	tabbedPane.setForeground(Attributes.textColor);
	contentPane.add(tabbedPane);

	panel = new JPanel();
	tabbedPane.addTab(Attributes.rs.getString("basic"), null, panel, null);
	panel.setLayout(null);
	panel.setBackground(Attributes.backgroundColor);
	panel.setForeground(Attributes.textColor);

	lblProtocol = new JLabel(Attributes.rs.getString("protocol") + ":");
	lblProtocol.setBounds(12, 12, 64, 15);
	lblProtocol.setBackground(Attributes.backgroundColor);
	lblProtocol.setForeground(Attributes.textColor);
	panel.add(lblProtocol);

	lblUser = new JLabel(Attributes.rs.getString("user") + ":");
	lblUser.setBounds(12, 39, 75, 14);
	lblUser.setBackground(Attributes.backgroundColor);
	lblUser.setForeground(Attributes.textColor);
	panel.add(lblUser);

	lblPassword = new JLabel(Attributes.rs.getString("password") + ":");
	lblPassword.setBounds(12, 66, 75, 15);
	lblPassword.setBackground(Attributes.backgroundColor);
	lblPassword.setForeground(Attributes.textColor);
	panel.add(lblPassword);

	protocolComboBox = new JComboBox();
	protocolComboBox.addFocusListener(new ProtocolComboBoxFocusListener());
	protocolComboBox.setModel(new DefaultComboBoxModel(new String[] {"XMPP", "GoogleTalk"}));
	protocolComboBox.setBounds(94, 7, 124, 24);
	protocolComboBox.setBackground(Attributes.backgroundColor);
	protocolComboBox.setForeground(Attributes.textColor);
	panel.add(protocolComboBox);

	UserTextField = new JTextField();
	UserTextField.setBounds(94, 37, 124, 19);
	UserTextField.setBackground(Attributes.backgroundColor);
	UserTextField.setForeground(Attributes.textColor);
	panel.add(UserTextField);
	UserTextField.setColumns(10);

	PasswordField = new JPasswordField();
	PasswordField.setBounds(94, 64, 124, 19);
	PasswordField.setBackground(Attributes.backgroundColor);
	PasswordField.setForeground(Attributes.textColor);
	panel.add(PasswordField);

	rdbtnSavePassword = new JRadioButton(Attributes.rs.getString("savePassword"));
	rdbtnSavePassword.setBounds(94, 90, 187, 23);
	rdbtnSavePassword.setBackground(Attributes.backgroundColor);
	rdbtnSavePassword.setForeground(Attributes.textColor);
	panel.add(rdbtnSavePassword);

	panel_1 = new JPanel();
	tabbedPane.addTab(Attributes.rs.getString("advanced"), null, panel_1, null);
	panel_1.setBackground(Attributes.backgroundColor);
	panel_1.setForeground(Attributes.textColor);
	panel_1.setLayout(null);

	lblServer = new JLabel(Attributes.rs.getString("server") + ":");
	lblServer.setBounds(12, 12, 51, 15);
	lblServer.setBackground(Attributes.backgroundColor);
	lblServer.setForeground(Attributes.textColor);
	panel_1.add(lblServer);

	lblPort = new JLabel(Attributes.rs.getString("port") + ":");
	lblPort.setBounds(12, 63, 35, 15);
	lblPort.setBackground(Attributes.backgroundColor);
	lblPort.setForeground(Attributes.textColor);
	panel_1.add(lblPort);

	lblSecurity = new JLabel(Attributes.rs.getString("security") + ":");
	lblSecurity.setBounds(12, 89, 63, 15);
	lblSecurity.setBackground(Attributes.backgroundColor);
	lblSecurity.setForeground(Attributes.textColor);
	panel_1.add(lblSecurity);

	ServerTextField = new JTextField();
	ServerTextField.setBounds(81, 10, 156, 19);
	ServerTextField.setBackground(Attributes.backgroundColor);
	ServerTextField.setForeground(Attributes.textColor);
	panel_1.add(ServerTextField);
	ServerTextField.setColumns(10);

	PortTextField = new JTextField();
	PortTextField.setText("5222");
	PortTextField.setBounds(81, 60, 63, 19);
	PortTextField.setBackground(Attributes.backgroundColor);
	PortTextField.setForeground(Attributes.textColor);
	panel_1.add(PortTextField);
	PortTextField.setColumns(10);

	SecuritycomboBox = new JComboBox();
	SecuritycomboBox.setBounds(81, 84, 156, 24);
	SecuritycomboBox.setBackground(Attributes.backgroundColor);
	SecuritycomboBox.setForeground(Attributes.textColor);
	panel_1.add(SecuritycomboBox);

	lblDomain = new JLabel("Domain:");
	lblDomain.setBounds(12, 38, 46, 14);
	lblDomain.setBackground(Attributes.backgroundColor);
	lblDomain.setForeground(Attributes.textColor);
	panel_1.add(lblDomain);

	DomainTextField = new JTextField();
	DomainTextField.setBounds(81, 35, 156, 20);
	DomainTextField.setBackground(Attributes.backgroundColor);
	DomainTextField.setForeground(Attributes.textColor);
	panel_1.add(DomainTextField);
	DomainTextField.setColumns(10);

	btnAdd = new JButton(Attributes.rs.getString("add"));
	btnAdd.addMouseListener(new BtnAddMouseListener());
	btnAdd.setBounds(10, 165, 117, 25);
	btnAdd.setBackground(Attributes.backgroundColor);
	btnAdd.setForeground(Attributes.textColor);
	contentPane.add(btnAdd);

	btnCancel = new JButton(Attributes.rs.getString("cancel"));
	btnCancel.addMouseListener(new BtnCancelMouseListener());
	btnCancel.setBounds(137, 165, 117, 25);
	btnCancel.setBackground(Attributes.backgroundColor);
	btnCancel.setForeground(Attributes.textColor);
	contentPane.add(btnCancel);
    }

    /**
     * Runs the Frame.
     */
    public static void run() {
	try {
	    NewProfile frame = new NewProfile();
	    frame.setVisible(true);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * Creates a new Profile with the Information entered by the User
     */
    private class BtnAddMouseListener extends MouseAdapter {
	@Override
	public void mousePressed(MouseEvent e) {
	    Profile profile;

	    int protocolIndex = protocolComboBox.getSelectedIndex();
	    String protocol = "";
	    String user = UserTextField.getText();
	    char[] password = PasswordField.getPassword();
	    String server = ServerTextField.getText();
	    String domain = DomainTextField.getText();
	    String portString = PortTextField.getText();
	    int port = Integer.parseInt(portString);
	    char[] empty = new char[1];
	    //int securityIndex = SecuritycomboBox.getSelectedIndex();
	    String security = "" + 1;

	    switch(protocolIndex)
	    {
	    case 0: protocol = "XMPP"; break;
	    case 1: 
		protocol = "GTALK"; 
		user = user + "@" + domain;
		break;
	    }
	    if(rdbtnSavePassword.isSelected()){
		javax.swing.JOptionPane.showMessageDialog(null, Attributes.rs.getString("passwordSavedLocally"), Attributes.rs.getString("warning"), javax.swing.JOptionPane.WARNING_MESSAGE);
	    }
	    if(rdbtnSavePassword.isSelected()) profile = new Profile(protocol, user, password, server, domain, port, security);
	    else profile = new Profile(protocol, user, empty, server, domain, port, security);
	    profile.saveProfile();
	    ContactList.profiles.add(profile);
	    ContactList.saveProfiles();

	    setVisible(false);
	    dispose();
	}
    }

    /**
     * Standard cancel operation.
     */
    private class BtnCancelMouseListener extends MouseAdapter {
	@Override
	public void mousePressed(MouseEvent e) {
	    setVisible(false);
	    dispose();
	}
    }

    /**
     * Autofill method for the DomainTextField and the ServerTextField
     */
    private class ProtocolComboBoxFocusListener extends FocusAdapter {
	@Override
	public void focusLost(FocusEvent arg0) {
	    switch(protocolComboBox.getSelectedIndex()){
	    case 0: 
		DomainTextField.setText(" ");
		ServerTextField.setText(" ");
		break;
	    case 1: 
		DomainTextField.setText("gmail.com");
		ServerTextField.setText("talk.google.com");
		break;
	    }
	}
    }

}
